import json
import sys

import facade
import pymysql
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
configfile = BaseDir.get_new_path(curPath, "db.ini")


class Step1DownSubject(object):
    def __init__(self):
        self.logger = facade.get_streamlogger()
        self.mysqlutile = facade.MysqlUtiles(configfile, "db", logger=self.logger)
        self.header = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "Origin": "http://zq.bookan.com.cn",
            "Referer": "http://zq.bookan.com.cn/?t=category&id=23579&ct=1&cid=42981&cur=1",
            # "Accept-Encoding": "gzip, deflate",
            # "Accept-Language": "zh-CN,zh;q=0.9",
            # "Cache-Control": "no-cache",
            # "Connection": "keep-alive",
            # "Host": "zq.bookan.com.cn",
            # "Pragma": "no-cache",
            # "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36"
        }

    def down_and_para_subject(self):
        url = "http://user.bookan.com.cn/index.php?op=Organization.specialWebMain&instanceId=23579&types=instanceInfo%2CwebConfigs%2CbookCategoryList%2CmagazineCategoryList"
        BoolResult, errString, r = facade.BaseRequest(url,
                                                      endstring="",
                                                      headers=self.header,
                                                      timeout=(30, 60),
                                                      mark="magazineCategoryList")
        if BoolResult:
            listsql = []
            jsondicts = json.loads(r.text)
            subjects = jsondicts["data"]["magazineCategoryList"]
            url = "http://user.bookan.com.cn/index.php?op=Resource.categoryIssues&instanceId=23579&resourceType=1&categoryId={}&pageNum=1&limitNum=20"
            if subjects:
                for onesub in subjects:
                    idtop = onesub['id']
                    nametop = onesub['name']
                    urltop = url.format(idtop)
                    sublevels = onesub['sublevels']
                    if sublevels:
                        for sub in sublevels:
                            idsub = sub['id']
                            namesub = sub['name']
                            urlsub = url.format(idsub)
                            sql = "replace into `subject` (`nametop`,`namesub`,`url`,`subjid`) values ('%s','%s','%s','%s')"
                            sql = sql % (
                                pymysql.escape_string(nametop), pymysql.escape_string(namesub),
                                pymysql.escape_string(urlsub),
                                idsub)
                            self.logger.info(sql)
                            listsql.append(sql)
                    else:
                        sql = "replace into `subject` (`nametop`,`namesub`,`url`,`subjid`) values ('%s','%s','%s','%s')"
                        sql = sql % (pymysql.escape_string(nametop), '', pymysql.escape_string(urltop), idtop)
                        self.logger.info(sql)
                        listsql.append(sql)
                self.mysqlutile.ExeSqlListToDB(listsql)

            else:
                print("下载失败 请检查")
                sys.exit(-1)


def main():
    down = Step1DownSubject()
    down.down_and_para_subject()


if __name__ == "__main__":
    main()
